<?php

namespace App\Http\Controllers\Weixin;

use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;

class UserController extends Controller 
{
    /**
     * 展示给定用户的信息。
     *
     * @param  int  $id
     * @return Response
     */
    public function show() 
    { 
        $options = [
            'debug'  => true,
            'app_id' => 'wxec85aea7e167cca2',
            'secret' => '987095cd9957af1771e97eab081e966e',
            'token'  => 'hello1001',
            'aes_key' => 'a7vueMQrygabKrlNHZri4kUnxatx6qQREEeSBOwhcuh', // 可选
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ], 
        ];
        $app = new Application($options);
        $server = $app->server;
        $server->setMessageHandler(function($message){
            switch ($message->MsgType) {
                case 'event':
                    if ($message->MsgType == 'event') {
                        switch ($message->Event) {
                            case 'subscribe':
                                // $list=\DB::table('admin_users')->insertGetId( 
                                //   ['pid' => $message->EventKey, 'sid' => $message->FromUserName]
                                // ); 
                                if($message->EventKey){
                                    $id=substr(strstr($message->EventKey, '_'),1);
                                }
                                $list=\DB::table("admin_users")->where("id",$id)->update(['openid' =>$message->FromUserName]); 
                                // if($list){
                                //     return $list;
                                //     break;
                                // }
                                return "朝霞为伴，夕阳作陪，滴滴汗水皆可贵！";
                                break;
                            case 'unsubscribe':
                            	$list=\DB::table("admin_users")->where("openid",$message->FromUserName)->update(['openid' =>'']);
                                return "谢谢取关!";
                                break;
                            default:

                                break;
                        }
                    }
                    // return '收到事件消息';
                    // break;
                case 'text':
                    return 'success'; 
                    break;
                case 'image':
                    return 'success';
                    break;
                case 'voice':
                    return 'success';
                    break;
                case 'video':
                    return 'success';
                    break;
                case 'location':
                    return 'success';
                    break;
                case 'link':
                    return 'success';
                    break;
                // ... 其它消息
                default:
                    return 'success';
                    break;
            }
        });
        $response = $server->serve(); 
        return $response;
        // Log::info('return response.');
        // return $wechat->server->serve();
    }
    // 生成临时二维码
    public function info()
    {
        // dd(\DB::table("admin_users")->get());
         
        $wechat = app('wechat');
        $qrcode = $wechat->qrcode;
        $result = $qrcode->temporary(56, 6 * 24 * 3600);
        $ticket = $result->ticket;
        $url = $qrcode->url($ticket);  
        return '<img src="'.$url.'" alt="临时二维码">';
    }
}